package com.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

//import org.apache.commons.httpclient.HttpClient;
//import org.apache.commons.httpclient.NameValuePair;
//import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
//import com.alibaba.fastjson.JSONObject;

//import net.sf.json.JSONObject;
public class otatest3 {
	public static AtomicInteger j = new AtomicInteger(0);
	public static void main1(String[] args) throws ClientProtocolException, IOException {
		ExecutorService pool = Executors.newCachedThreadPool();
		for (int i = 0; i < 3; i++) {
		pool.execute(new Runnable() {
			@Override 
			public void run() { //21个
	  			try { 
	  				download3("C:\\Users\\17M38332\\Desktop\\1\\file\\" + j.incrementAndGet() + ".zip");
	  			}
	  			catch(Exception e) { 
	  				e.printStackTrace(); 
	  			}
	  		}
		});
		
		}
			
	}

	public static void download3(String localFileName) {
		FileOutputStream out = null;
		InputStream is = null;
		try {
			HttpPost httpPost = new HttpPost("http://127.0.0.1:8086/app/do");
//			HttpPost httpPost = new HttpPost("http://sfota.swmmotors.com.cn/upgrade/apitbox/download");//100只有13个是全的
//			HttpPost httpPost = new HttpPost("http://vota.swmmotors.com.cn:8086/upgrade/app/do");//100个有20个
			CloseableHttpClient client = HttpClients.createDefault();
			String respContent = null;

			/*JSONObject jsonParam = new JSONObject();
			jsonParam.put("token", "123456");
			jsonParam.put("id", "OOXXYWTESTXXOO");
			jsonParam.put("filename", "FP_ARM_105.zip");
			//jsonParam.put("filename", "FP_MCU_119.bin");
			jsonParam.put("path", "TBOX/300/300/20180718104400");
			//jsonParam.put("path", "TBOX\\300\\300\\20180726195935");
			StringEntity s = new StringEntity(jsonParam.toString(), "utf-8");
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");

			httpPost.setEntity(s);*/

			HttpResponse resp = client.execute(httpPost);

			/*if (resp.getStatusLine().getStatusCode() == 200) {
				HttpEntity he = resp.getEntity();
				respContent = EntityUtils.toString(he, "UTF-8");
			}*/

			// connect to server (tcp) httpURLConnection.connect();

			HttpEntity entity = resp.getEntity();
			is = entity.getContent();
			// server
			File file = new File(localFileName);
			if (!file.exists()) {
				file.createNewFile();
			}

			out = new FileOutputStream(file);
			byte[] buffer = new byte[4096];
			int readLength = 0;
			while ((readLength = is.read(buffer)) > 0) {
				byte[] bytes = new byte[readLength];
				System.arraycopy(buffer, 0, bytes, 0, readLength);
				out.write(bytes);
			}

			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}